package com.jstyle.jclifexd.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jstyle.jclifexd.NetWork.NetWorkUtil;
import com.jstyle.jclifexd.R;
import com.jstyle.jclifexd.model.NetResultData;

import com.jstyle.jclifexd.utils.NetWorkConast;
import com.jstyle.jclifexd.utils.SHA1;
import com.jstyle.jclifexd.view.EditTextWithDelete;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

public class ForgotPwdActivity extends BaseActivity {

    @BindView(R.id.tv_forgotpwd_title)
    TextView tvForgotpwdTitle;
    @BindView(R.id.tv_register_tips)
    TextView tvRegisterTips;
    @BindView(R.id.edit_forgotpwd_email)
    EditTextWithDelete editForgotpwdEmail;
    @BindView(R.id.bt_forgotpwd_back)
    Button btForgotpwdBack;
    @BindView(R.id.bt_reset_pwd)
    Button btResetPwd;
    @BindView(R.id.bt_back)
    Button btBack;
    private Disposable resetDisposable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_forgot_pwd);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.bt_forgotpwd_back, R.id.bt_reset_pwd, R.id.bt_back})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.bt_forgotpwd_back:
                finish();
                break;
            case R.id.bt_reset_pwd:
                toResetPwd();
                break;
            case R.id.bt_back:
                finish();
                break;
        }
    }

    private void toResetPwd() {
        String email=editForgotpwdEmail.getText().toString();
        if(TextUtils.isEmpty(email)){
            showToast(getString(R.string.fill_all_info));
            return;
        }
        if (!NetWorkUtil.isNetworkConnected(this)) {
            showToast(getString(R.string.Network_not_availa));
            return;
        }
        Map<String, Object> map = new HashMap<>();
        map.put(NetWorkConast.KEY_userEmail, email);

        NetWorkUtil.addSHA1(map);
        NetWorkUtil.getInstance().getJstyleApi().findPassword(map).subscribeOn(Schedulers.io()).doOnSubscribe(new Consumer<Disposable>() {
            @Override
            public void accept(Disposable disposable) throws Exception {
                resetDisposable =disposable;
                showProgressDialog(getString(R.string.smssdk_wait));
            }
        }).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<NetResultData>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(NetResultData netResultData) {
                disMissProgressDialog();
                int msgCode = netResultData.getMsgCode();
                String msg = NetWorkConast.getResult(msgCode, ForgotPwdActivity.this);
                if (msgCode == NetResultData.Success_Code) {
                 //   msg = getString(R.string.login_complete);
                    sendEmailSuccessful();
                }else{
                    showToast(msg);
                }
            //
            }

            @Override
            public void onError(Throwable e) {
                disMissProgressDialog();
            }

            @Override
            public void onComplete() {

            }
        });

    }
    private void sendEmailSuccessful(){
        btBack.setVisibility(View.VISIBLE);
        btForgotpwdBack.setVisibility(View.GONE);
        btResetPwd.setVisibility(View.GONE);
        editForgotpwdEmail.setVisibility(View.GONE);
        tvForgotpwdTitle.setText("Check Your Email");
        tvRegisterTips.setText("We sent you an email with a reset link.");
    }


    private void showErrorMsg(){
        //showToast();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unSubscribe(resetDisposable);
    }
}
